<template>
	<wsModal v-model="modal.show" title="角色关联中转仓 设置" @on-ok="saveModal" @on-cancel="cancelModal">
		<Transfer :data="modal.datas" 
				  :target-keys="modal.targetKeys" 
				  @on-change="handleChange">
		</Transfer>
	</wsModal>
</template>

<script>
	export default {
		data() {
			return {
				modal: {
					show: false,
					roleId: '',
					datas: [],
					targetKeys: []
				}
			}
		},
		mounted() {
		},
		methods: {
			show(roleId) {
				this.modal.roleId = roleId;
				let _this = this;
				this.axios.get('/ws/ops/v2/transferstorehouses/role/' + roleId).then(response => {
					var res = response.data;
					var resMeta = res.meta;
					if(0 == resMeta.code) {
						_this.buildData(res.data);
					} else {
						_this.$Message.warning(resMeta.message);
					}
				});
			},
			buildData(data) {
				let transfers = data.all;
				let roleTransfers = data.selected;
				this.modal.datas = [];
				for(let i = 0; i < transfers.length; i++) {
					let transfer = transfers[i];
					this.modal.datas.push({
						key: transfer.wKey,
						label: transfer.wValue,
					});
				}
				this.modal.targetKeys = [];
				for(let i = 0; i < roleTransfers.length; i++) {
					let roleTransfer = roleTransfers[i];
					this.modal.targetKeys.push(roleTransfer.wKey);
				}
				this.modal.show = true;
			},
			cancelModal() {
				this.modal.show = false;
			},
			saveModal() {
				let postData = {
					roleId: this.modal.roleId,
					keys: this.modal.targetKeys
				};
				let _this = this;
				this.axios.put('/ws/ops/v2/transferstorehouses/role', postData).then(response => {
					var res = response.data;
					var resMeta = res.meta;
					if(0 == resMeta.code) {
						_this.$Message.success('保存成功');
					} else {
						_this.$Message.warning(resMeta.message);
					}
				});
			},
			handleChange(newTargetKeys) {
				this.modal.targetKeys = newTargetKeys;
			}
		}
	}
</script>

<style>

</style>